package org.jin.samples.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.jin.samples.dao.ITestDao;
import org.jin.samples.model.ExamTest;
import org.jin.samples.model.vo.ExamTestTypeVO;
import org.jin.samples.model.vo.ExamTestVO;
import org.jin.samples.page.PagerModel;
import org.jin.samples.page.SystemContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class TestDaoImpl extends BaseHibernateDAO<ExamTest> implements ITestDao{
		
	@SuppressWarnings("unchecked")
	public PagerModel<ExamTest> findAllByPage(){
		PagerModel<ExamTest> page = new PagerModel<ExamTest>();
		ArrayList<ExamTest> dates = new ArrayList<ExamTest>();
		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getPagesize();
		System.out.println("offset="+offset);
		System.out.println("pageSize"+pageSize);
		//是用hibernate完成分页
		List<ExamTest> tempDates = this.getSession().createQuery(" from ExamTest")
				.setFirstResult(offset)
				.setMaxResults(pageSize)
				.list();
		
		for(ExamTest examTest : tempDates){				
			dates.add(examTest);
		}
		
		page.setDates(dates);
		page.setOffset(offset);
		page.setPageSize(pageSize);
		
		//获取总记录数
		long totalSize = (long) this.getSession().createQuery("select count(*) from ExamTest")
				.uniqueResult();
		page.setTotalSize(totalSize);
		return page;
		
	}
	
	
	@Override
	public List<ExamTest> findAllTest(){
		String hql = "FROM ExamTest";
		return this.find(hql);
	}
	
	@Override
	public ExamTest findByTest(Integer id){
		ExamTest examTest = this.get(id);
		return examTest;	
	}
	
	@Override
	public void editTest(ExamTest test)throws Exception{
		this.saveOrUpdate(test);
	}
	
	@Override
	public void deleteUser(ExamTest test){
		 this.delete(test);
	}


	@Override
	public List<ExamTest> getTestListBySubAndTestType(Integer subId, Integer testTypeId) {
		String hql=" from ExamTest where sub_id = ? and test_type_id = ?";
		List<ExamTest> testList = this.queryForList(hql, new Object[] {subId, testTypeId});
		return testList;
	}

	public List<ExamTest> findAllBySubId(Integer subId){
		String hql = "from ExamTest where sub_id =?";
		return this.queryForList(hql, new Object[]{subId});
	}
	
	
	
}
